package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Project;
import com.manger.model.SmallMi;
import com.manger.utils.*;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SmallMiCaseFilesFromat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/2 13:28
 * @Version 1.0
 */
public class SmallMiCaseFilesFormat implements CaseFilesFormat{

    private static Map<String, LoanFileEnum> filesMapping = new HashMap<>();

    private List<String> onlyOneFileNameMarks = new ArrayList<>();

    private static String ocrIdCardMark = "被告身份证";

    private Map<String, String> idCardMap = new HashMap<>();

    {
        //  filesMapping.put("被告身份证", LoanFileEnum.PART_IDENTITY_CERTIFICATE);
        onlyOneFileNameMarks.add("身份证及人脸识别");
    }

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        List<String> ignoreFileList = project.getIgnoreFilesList();
        List<String> ignoreDirList = project.getIgnoreDirList();
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        Map<String,List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelFilePath), SmallMi.class).get();
        if(map.keySet().size() <= 0){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<SmallMi> smallMiList = (List<SmallMi>) map.get(map.keySet().iterator().next());
        if(smallMiList == null){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<SmallMi> errorList = new ArrayList<>();
        for(SmallMi smallMi: smallMiList){
            if (StringUtils.isEmpty(smallMi.getIdCard())){
                errorList.add(smallMi);
                continue;
            }
            String dirName = smallMi.getLoanUserName() + smallMi.getIdCard().substring(smallMi.getIdCard().length() - 4,smallMi.getIdCard().length());
            String resourcePath = extractDir + "/" + project.getDebtName() + "/" + dirName;
            String tempTargetDir = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME
                    + "/" + smallMi.getLoanUserName() + "-" + smallMi.getIdCard();
            try{
                File file = new File(resourcePath);
                if(!file.exists()){
                    continue;
                }
                CopyFiles.createDirs(tempTargetDir);
                File[] tempFiles = file.listFiles();
                for(File tempFile : tempFiles){
                    if (tempFile.isFile() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreFileList)) {
                        continue;
                    }
                    if (tempFile.isDirectory() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreDirList)) {
                        continue;
                    }
                    String targetRealPath = tempTargetDir + "/" + CopyFiles.getLoanFileEnum(tempFile.getName(),filesMapping).getName();
                    if (!CopyFiles.checkHasCopyFileToDirByOnlyOneFileNameMarks(tempFile.getName(), onlyOneFileNameMarks,targetRealPath)) {
                        continue;
                    }
                    CopyFiles.copyFileToDir(tempFile.getAbsolutePath(),targetRealPath );
                    idCardMap.put(smallMi.getIdCard(),tempTargetDir);
                }
                //拷贝统一数据
                CopyFiles.copyBaseFiles(tempTargetDir, project);
                System.out.println("整理原始数据成功：【" + dirName + "】");
            }catch (Exception e){
                e.printStackTrace();
                errorList.add(smallMi);
                System.out.println("整理原始数据失败：【" + dirName + "】");
            }
        }
        //处理数据u idCardMap : 身份证：地址
        CopyFiles.copyOtherMaterialFileToDir(extractDir,idCardMap);

        if(!errorList.isEmpty()){
            String errorFilePath = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.ERROR_EXCEL_PRE + System.currentTimeMillis() + ".xlsx";
            ExcelFactory.getExcelFactory().getExportExecutor().sheet("整理错误")
                    .data(errorList).head(SmallMi.class).write(new FileOutputStream(errorFilePath));
            System.out.println("导出整理错误数据【"+errorFilePath+"】");
        }
    }

    public static void main(String[] args) throws Exception {
        ChinaAreaTools.init();
        ProjectUtils.init();
        SmallMiCaseFilesFormat smallMiCaseFilesFormat = new SmallMiCaseFilesFormat();
        String target = SystemConstant.getFormatDir() + "/" + "target" + "/" + "123456";
        String extractDir = "D:\\workSpace\\AutomaticCase\\AutomaticFiling/../format\\1756714714644\\小米";
        smallMiCaseFilesFormat.format(extractDir,target,ProjectUtils.projectMap.get(DetNameEnum.XM));
    }
}
